<template>
  <div class="login">
    <div class="login-panel">
      <h2 class="login-title tc">登录</h2>
      <el-form :model="form" ref="form" class="mt20">
        <el-form-item prop="account" :rules="{validator:validator_userName,trigger:'blur'}">
          <el-input v-model="form.account"  @keyup.enter.native="submit('form')" placeholder="用户名">
            <template slot="prepend">
              <img :src="require('@/assets/images/user.png')" width="15" height="15">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" :rules="{validator:validator_pass,trigger:'blur'}">
          <el-input type="password" v-model="form.password" @keyup.enter.native="submit('form')" placeholder="密码">
            <template slot="prepend">
              <img :src="require('@/assets/images/pass.png')" width="15" height="15">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;" @click="submit('form')" :loading="loading">登录</el-button>
        </el-form-item>
        <el-form-item class="register-pass">
          <span>立即注册</span>
          <span>忘记密码</span>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import * as s from '@/components/utils/sessionStorage'
import { login} from '@/components/api/account'
import axios from 'axios'
import md5 from 'js-md5'
// import { mapGetters } from 'vuex'
export default {
  name: 'login',
  components: {
  },
  data() {
    return {
      loading: false,
      rememberPass: false,
      form: {
        account: '',
        password: ''
      }
    }
  },
  // computed: {
  //   ...mapGetters(['flagShow'])
  // },
  methods: {
    validator_userName(rule, value, callback) {
      if (value === '' || value === undefined || value === null) {
        callback(new Error('请输入账号'));
      } else {
        callback()
      }
    },
    validator_pass(rule, value, callback) {
      if (value === '' || value === undefined || value === null) {
        callback(new Error('请输入密码'));
      } else {
        callback()
      }
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login()
        } else {
          return false;
        }
      })
    },
    login() {
      this.form.password = md5(this.form.password)
      let params = this.form;
      let _this=this;
        axios.post('http://192.168.0.128:8080/gxszy-auth/api/szy/sysmng/authentication/auth', this.form).then((res)=> {
        _this.$store.dispatch('setMenu', res.data.result.userData)
        s.set('index', 0); // 设置头部下标
        // 判断用户身份
        s.set('token', res.data.result.token);
        let url = res.data.result.userData.modules[0].URL;
        console.log(res.data.result)
        _this.$message({
          type: "success",
          duration:1000,
          message: '登录成功'
        })
        if(!!url) {
          console.log(this.$store)
          _this.$store.dispatch('setFlagShow', true)
        }
          this.$router.push(url)
      }).catch((error) => {
        this.$message({
          type: "error",
          message: '登录失败'
        })
      });
    }
  },
  created() {
  },
  mounted() {

  }
}
</script>

<style lang='less' scoped>
.login {
  width: 100%;
  height: 100%;
  position: relative;
//   background: url('../assets/images/bg.jpg') no-repeat;
  background-size: cover
}

.login-panel {
  background-color: #ffffff;
  border-radius: 9px;
  width: 400px;
  padding: 35px 35px 15px 35px;
  color: #409EFF;
  position: absolute;
  right: 160px;
  top: 50%;
  transform: translateY(-50%);
}

.login-title {
  font-size: 26px;
  font-weight: 400;
  margin: 0px auto 40px auto;
  text-align: center;
  font-weight: bold;
}
.register-pass{
  color: #999;
  span{
    margin-right: 10px;
    cursor: pointer;
  }
}
</style>
